package com.wxmit.module.system.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wxmit.base.enums.BusinessStatus;
import com.wxmit.framework.satoken.core.util.StpUserUtil;
import com.wxmit.module.system.domain.SysLoginLog;
import com.wxmit.module.system.domain.vo.LoginLogVO;
import com.wxmit.module.system.mapper.SysLoginLogMapper;
import com.wxmit.module.system.service.ISysLoginLogService;
import com.wxmit.system.base.core.ShushanPage;
import com.wxmit.system.base.core.ShushanServiceImpl;
import com.wxmit.system.base.domain.PageDTO;
import com.wxmit.system.domain.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description SysLoginLogServiceImpl
 * @Author huangxiong
 * @Date 2022/7/13 14:21
 * @Version V1.0
 */
@Service
public class SysLoginLogServiceImpl extends ShushanServiceImpl<SysLoginLogMapper, SysLoginLog> implements ISysLoginLogService {

    @Autowired
    private SysLoginLogMapper loginLogMapper;

    @Async
    @Override
    public void saveWithAsync(LoginUser loginUser, BusinessStatus status,String msg){
        SysLoginLog sysLoginLog = new SysLoginLog();
        // 用户相关
        sysLoginLog.setUserId(loginUser.getId());
        sysLoginLog.setUserName(loginUser.getUsername());
        //环境相关
        sysLoginLog.setIpaddr(loginUser.getIpaddr());
        sysLoginLog.setLoginLocation(loginUser.getLoginLocation());
        sysLoginLog.setBrowser(loginUser.getBrowser());
        sysLoginLog.setOs(loginUser.getOs());
        sysLoginLog.setLoginTime(new Date());
        sysLoginLog.setStatus(status.code);
        sysLoginLog.setMsg(msg);
        save(sysLoginLog);
    }

    @Override
    public IPage<SysLoginLog> listPage(PageDTO<SysLoginLog> pageDto) {
        pageDto.getPage().setUnderlineCaseOrderIfNull("loginTime",true);
        return loginLogMapper.listPage(pageDto.getPage(),pageDto.getSearchData());
    }

    @Override
    public void clear() {
        loginLogMapper.clear();
    }

    @Override
    public List<LoginLogVO> workBenchLoginLog() {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        List<String> dateList = new ArrayList<>();
        dateList.add(dateFormat.format(calendar.getTime()));
        for (int i = 0; i < 6; i++) {
            calendar.add(Calendar.DAY_OF_YEAR,-1);
            dateList.add(dateFormat.format(calendar.getTime()));
        }
        List<LoginLogVO> loginLogVOS = loginLogMapper.workBenchLoginLog(StpUserUtil.getLoginId(), dateList);
        for (String s : dateList) {
            boolean exist = false;
            for (LoginLogVO loginLogVO : loginLogVOS) {
                if(s.equals(loginLogVO.getDate())){
                    exist = true;
                    break;
                }
            }
            if(!exist){
                loginLogVOS.add(new LoginLogVO(s,0));
            }
        }
        return loginLogVOS.stream().sorted(Comparator.comparing(LoginLogVO::getDate)).collect(Collectors.toList());
    }
}
